package com.ziki.bswicket.template.crud.service;

import java.util.ArrayList;
import java.util.List;

import com.ziki.bswicket.template.crud.model.CrudModel;
import com.ziki.common.util.string.StrUtil;

public class CrudService {
    private static List<CrudModel> lst = null;
    static {
        lst = new ArrayList<CrudModel>();
        for (int i = 0; i < 55; i++) {
            CrudModel article = new CrudModel();
            article.setId(StrUtil.parseStr(i));
            article.setTitle("title" + i);
            article.setCrdate("2014-05-" + i);
            lst.add(article);
        }
    }

    public CrudService() {
    }

    public long getDataCount(CrudModel model) {
        System.out.println("getDataCount");
        return lst.size();
    }

    public List<CrudModel> getPageList(CrudModel model, long first, long count) {
        System.out.println("getPageList");

        long toIndex = first + count;
        long dataCount = lst.size();
        if (toIndex > dataCount) {
            toIndex = dataCount;
        }
        ArrayList<CrudModel> arrayList = new ArrayList<CrudModel>();
        arrayList.addAll(lst.subList((int) first, (int) toIndex));
        return arrayList;
    }

    public void add(CrudModel e) {
        System.out.println("add");
        lst.add(e);
    }

    public void edit(CrudModel e) {
        System.out.println("edit");
        for (CrudModel model : lst) {
            if (model.getId().equals(e.getId())) {
                model.setTitle(e.getTitle());
                model.setCrdate(e.getCrdate());
            }
        }
    }

    public CrudModel getArticle(String id) {
        System.out.println("getArticle");
        for (CrudModel model : lst) {
            if (model.getId().equals(id)) {
                return model;
            }
        }
        return null;
    }
}
